package cn.yili12.business.controller;

import cn.yili12.business.dto.OrganizeAddDTO;
import cn.yili12.business.dto.PageInfo;
import cn.yili12.business.entity.OrganizeDb;
import cn.yili12.business.entity.UserDb;
import cn.yili12.business.service.OrganizeService;
import cn.yili12.business.vo.Result;
import cn.yili12.common.thread.AgentThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/organize")
@Api(value = "分组管理")
@Slf4j
public class OrganizeController {
    @Autowired
    private OrganizeService service;

    @GetMapping("/page")
    @ApiOperation("同意组用户列表")
    public Result page(HttpServletRequest request,
                       @ApiParam("当前页码") @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                       @ApiParam("分页大小") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        UserDb userDb = (UserDb) AgentThreadLocal.get();
        Page<OrganizeDb> all = service.findAll(pageNo, pageSize);
        return Result.success("获取分组数据成功", new PageInfo<OrganizeDb>(all));
    }

    @PostMapping("/add")
    @ApiOperation("添加分组")
    public Result friends(@Valid OrganizeAddDTO organizeAddDTO) {
        // TODO 判断组织编码是否存在
        UserDb userDb = (UserDb) AgentThreadLocal.get();
        OrganizeDb organizeDb = new OrganizeDb();
        organizeDb.setName(organizeAddDTO.getName());
        service.save(organizeDb);
        return Result.success("添加分组成功", organizeDb);
    }


    @GetMapping("/delete/{id}")
    @ApiOperation("删除分组")
    public Result friends(@PathVariable("id") Long id) {
        UserDb userDb = (UserDb) AgentThreadLocal.get();
        service.delete(id);
        return Result.success("删除分组成功", null);
    }
}
